/*
* $Id: SettingsWithUserColumn.cpp 152 2010-09-23 18:28:55Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "SettingsWithUserColumn.h"
#include "../SettingControlIds.h"
#include "Settings/DinIntDefined.h"
#include "Settings/Journal/ListBoxStateParser.h"
#include "Visual/Settings/ColumnDialog.h"
#include "Database/Tables/UserFieldsTable.h"

CSettingsWithUserColumn::CSettingsWithUserColumn(int nIdString, const std::tstring& strRegName, bool bHeader)
: CSettings(nIdString, strRegName, bHeader), m_bChange(false){}
CSettingsWithUserColumn::~CSettingsWithUserColumn(){}

std::map<std::tstring, std::tstring> CSettingsWithUserColumn::GetStates()
{
	std::map<std::tstring, std::tstring> aStates;
	aStates.insert(aStates.end(), std::make_pair(STATIC_HEADER2_ID, ILanguageManager::GetLanguageManager()->GetString(USER_COLUMN_ID)));
	aStates.insert(aStates.end(), std::make_pair(ADD_COLUMN_ID, BUTTON_STATE_2));
	aStates.insert(aStates.end(), std::make_pair(DELETE_COLUMN_ID, BUTTON_STATE_3));
	return aStates;
}

std::map<std::tstring, std::vector<Parameter> > CSettingsWithUserColumn::GetParameters()
{
	std::map<std::tstring, std::vector<Parameter> > aMapParameters;
	std::vector<Parameter> aParameters;

	//static_header2
	aParameters.push_back(Parameter(TYPE, HEADER));
	aMapParameters.insert(aMapParameters.end(), std::make_pair(STATIC_HEADER2_ID, aParameters));

	//list_column_user
	aParameters.clear();
	aParameters.push_back(Parameter(DEPENDENTS, DELETE_COLUMN_ID));
	IDatabase* pDatabase = IDatabaseManager::GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CUserFieldsTable table;
	table.m_content.s_table = GetTypeTable();
	IResult* result = table.GetValue(pDatabase);
	IDatabaseManager::GetDatabaseManager()->CloseDatabase(pDatabase);
	for (size_t i = 0; i < result->GetSizeResult(); i++){
		CUserFieldsTable::content* cnt = dynamic_cast<CUserFieldsTable::content*>((*result)[(int)i]);
		m_aTypeColumns.insert(m_aTypeColumns.end(), std::make_pair(cnt->s_strName, cnt->s_type));
		aParameters.push_back(Parameter(LIST_STRING, cnt->s_strName + _T("|") + CConvertFunction::ConvertFromInt(cnt->s_type)));
	}
	result->Free();
	aMapParameters.insert(aMapParameters.end(), std::make_pair(LIST_COLUMN_USER_ID, aParameters));

	//add_column
	aParameters.clear();
	aParameters.push_back(Parameter(DEPENDENTS, LIST_COLUMN_USER_ID));
	aParameters.push_back(Parameter(DEPENDENTS, DELETE_COLUMN_ID));
	aMapParameters.insert(aMapParameters.end(), std::make_pair(ADD_COLUMN_ID, aParameters));

	//delete_column
	aParameters.clear();
	aParameters.push_back(Parameter(DEPENDENTS, LIST_COLUMN_USER_ID));
	aParameters.push_back(Parameter(DEPENDENTS, DELETE_COLUMN_ID));
	aMapParameters.insert(aMapParameters.end(), std::make_pair(DELETE_COLUMN_ID, aParameters));
	return aMapParameters;
}

std::vector<Window> CSettingsWithUserColumn::GetWindows(){
	std::vector<Window> aWindows;
	CRect rect = GetSizeRegion();
	aWindows.push_back(Window(STATIC, rect.left, rect.top, 415, 23, SS_CENTER, ILanguageManager::GetLanguageManager()->GetString(USER_COLUMN_ID), STATIC_HEADER2_ID));
	aWindows.push_back(Window(LIST_BOX, rect.left, rect.top + 30, rect.Width(), rect.Height(), LBS_SORT | WS_BORDER | WS_VSCROLL, _T(""), LIST_COLUMN_USER_ID));
	aWindows.push_back(Window(BUTTON_ELEMENT, rect.right + 5, rect.top + 30, 80, 25, BS_CENTER, ILanguageManager::GetLanguageManager()->GetString(ADD_ID), ADD_COLUMN_ID));
	aWindows.push_back(Window(BUTTON_ELEMENT, rect.right + 5, rect.top + 60, 80, 25, BS_CENTER, ILanguageManager::GetLanguageManager()->GetString(DELETE_ID), DELETE_COLUMN_ID));
	return aWindows;
}

void CSettingsWithUserColumn::SaveSetting(){
	std::map<std::tstring, std::tstring>::iterator it;

	if((it = m_aChange.find(LIST_COLUMN_USER_ID)) != m_aChange.end()){
		CListBoxStateParser stateParser(it->second);
		std::vector<Col> aAddColumn  = stateParser.GetAddColumn();
		std::vector<Col> aDelColumn  = stateParser.GetDeleteColumn();
		IDatabase* pDatabase = IDatabaseManager::GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
		CUserFieldsTable table;
		pDatabase->BeginTransaction(_T("Add UserFields"));
		for(std::vector<Col>::iterator it1 = aDelColumn.begin(); it1 != aDelColumn.end(); it1++){
			table.m_content.s_table = GetTypeTable();
			table.m_content.s_strName = it1->m_strName;
			table.m_content.s_type = (TypeField::type)it1->m_nId;
			table.DeleteValue(pDatabase);
		}
		for(std::vector<Col>::iterator it1 = aAddColumn.begin(); it1 != aAddColumn.end(); it1++){
			table.m_content.s_table = GetTypeTable();
			table.m_content.s_strName = it1->m_strName;
			table.m_content.s_type = (TypeField::type)it1->m_nId;
			table.SetValue(pDatabase);
		}
		pDatabase->EndTransaction(_T("Add UserFields"));
		IDatabaseManager::GetDatabaseManager()->CloseDatabase(pDatabase);
		stateParser.SaveChange();
		m_aChange.erase(it);
		for(std::map<sWindow, std::tstring>::iterator it1 = m_aWindows.begin(); it1 != m_aWindows.end(); it1++){
			if(it1->first.m_window.m_strId == LIST_COLUMN_USER_ID)
				((sWindow&)it1->first).m_strState = stateParser.GetState();
		}
		m_bChange = true;
	}
	if((it = m_aChange.find(ADD_COLUMN_ID)) != m_aChange.end())
		m_aChange.erase(it);
	if((it = m_aChange.find(DELETE_COLUMN_ID)) != m_aChange.end()){
		for (std::map<sWindow, std::tstring>::iterator it1 = m_aWindows.begin(); it1 != m_aWindows.end(); it1++){
			if(it1->first.m_window.m_strId == it->first){
				it1->second = it->second;
				break;
			}
		}
		m_aChange.erase(it);
	}
	__super::SaveSetting();
}

bool CSettingsWithUserColumn::IsChanged(){
	for (std::map<std::tstring, std::tstring>::iterator it = m_aChange.begin(); it != m_aChange.end(); it++){
		if(it->first == LIST_COLUMN_USER_ID){
			CListBoxStateParser stateParser(it->second);
			if(stateParser.GetAddColumn().empty() && stateParser.GetDeleteColumn().empty())
				continue;
		}
		if(it->first == DELETE_COLUMN_ID || it->first == ADD_COLUMN_ID)
			continue;
		return true;
	}
	return m_bChange;
}

void CSettingsWithUserColumn::SetState(const std::tstring& strId, const std::tstring& strState){
	__super::SetState(strId, strState);
	if(strId == LIST_COLUMN_USER_ID){
		CListBoxStateParser stateParser(strState);
		if(stateParser.GetCurSel().empty())
			m_aChange[DELETE_COLUMN_ID] = BUTTON_STATE_3;
		else
			m_aChange[DELETE_COLUMN_ID] = BUTTON_STATE_2;
	}else	if(strId == ADD_COLUMN_ID && m_aChange[ADD_COLUMN_ID] != BUTTON_STATE_1){
		CColumnDialog dialog(_ADD);
		if(dialog.WindowCreate()){
			CUserFieldsTable::content column = dialog.GetColumn();
			CListBoxStateParser stateParser(GetState(LIST_COLUMN_USER_ID));
			stateParser.AddChange(column.s_strName, column.s_type, _ADD);
			stateParser.SetCurSel(column.s_strName);
			m_aChange[LIST_COLUMN_USER_ID] = stateParser.GetState();
			m_aChange[DELETE_COLUMN_ID] = BUTTON_STATE_2;
		}
		m_aChange[ADD_COLUMN_ID] = BUTTON_STATE_2;
	}else if(strId == DELETE_COLUMN_ID && m_aChange[DELETE_COLUMN_ID] != BUTTON_STATE_1){
		CListBoxStateParser stateParser(m_aChange[LIST_COLUMN_USER_ID].c_str());
		stateParser.AddChange(stateParser.GetCurSel(), m_aTypeColumns[stateParser.GetCurSel().c_str()], _DELETE);
		stateParser.SetCurSel(_T(""));
		m_aChange[LIST_COLUMN_USER_ID] = stateParser.GetState();
		m_aChange[DELETE_COLUMN_ID] = BUTTON_STATE_3;
	}
}

void CSettingsWithUserColumn::CancelChange(){
	m_bChange = false;
	__super::CancelChange();
}
